package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.varia.Roller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.SystemUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.MenuService;
import cn.com.hfuu.service.RoleMenuService;
import cn.com.hfuu.service.RoleService;
import cn.com.hfuu.service.UserroleService;
import cn.com.hfuu.vo.MenuVo;
import cn.com.hfuu.vo.Page;
import cn.com.hfuu.module.*;

@Controller
@RequestMapping("role")
public class RoleCotroller {

	@Autowired
	private CodeService codeservice;

	@Autowired
	private RoleService roleservice;

	@Autowired
	private MenuService menuservice;

	@Autowired
	private RoleMenuService rolemenuservice;
	
	@Autowired
	private UserroleService userroleService;

	private UUIdUtil id = new UUIdUtil();

	List<code> codelist = new ArrayList<code>();

	@RequestMapping("role")
	public String role() {

		codelist = codeservice.selectbyType("10");
		return "role/role";
	}

	@RequestMapping("rolelist")
	@ResponseBody
	public Page rolelisst(Integer start, Integer limit) {
		List<role> rolelist = roleservice.selectall(start, limit);
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(rolelist);
		page.setTotal(roleservice.selectalltotal());

		return page;
	}

	@RequestMapping("add")
	public String addrole() {
		return "role/roleadd";
	}

	@RequestMapping("addroleproc")
	@ResponseBody
	public String addroleproc(role r) {
		r.setRoleId(id.getUuid());
		if (roleservice.exitname(r.getRoleName()) == null) {
			roleservice.insert(r);
			return "success";
		} else {
			return "error";
		}
	}

	@RequestMapping("edit")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView();
		role r = roleservice.editbyid(id);
		mv.setViewName("role/roleedit");
		mv.addObject("role", r);
		return mv;

	}

	@RequestMapping("editproc")
	@ResponseBody
	public String editpro(role r) {
		if (roleservice.exitnameedit(r.getRoleName(), r.getRoleId()) != null) {
			return "error";
		}
		if (roleservice.update(r) > 0) {
			return "success";
		}
		return "false";
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String userdelete(String[] ids) {
		for (String string : ids) {
			System.out.println(string);
		}
		if (roleservice.delete(ids) > 0) {
			rolemenuservice.deleteroleid(ids);
			userroleService.deleterolid(ids);
			return "success";
		}
		return "false";

	}

	String roleid;

	@RequestMapping("roleedit")
	public String roleeditmenu(String id) {
		roleid = id;
		return "role/menu";
	}

	//在数据库中存在的菜单
	List<rolemenu> rolemenu;
	@RequestMapping("rolemenu")
	@ResponseBody
	public MenuVo[] rolemenuedit() {
		// 获取已有的菜单
		  rolemenu = rolemenuservice.selectrole(roleid);
		
		List<MenuVo> menuvolist1 = new ArrayList<MenuVo>();
		List<menu> menulist = menuservice.selectAll();// 一级菜单
		MenuVo[] menuvol = new MenuVo[menulist.size()];
		for (int j = 0; j < menulist.size(); j++) {
			menu menu = menulist.get(j);// 循环一级菜单
			MenuVo menuvo = new MenuVo();
			menuvo.setTitle(menu.getMenuName());
			menuvo.setValue(menu.getMenuId());
			menuvo.setChecked(false);
			for (rolemenu rolemenu1 : rolemenu) {// 循环已有的菜单
				 
				if (rolemenu1.getMenuId().equals(menu.getMenuId())) {
					menuvo.setChecked(true);
				}
			}
			List<menu> menulist1 = rolemenuservice.selectSeconnd(menu
					.getMenuId());// 二级菜单
			MenuVo[] menuvolist2 = new MenuVo[menulist1.size()];
			for (int i = 0; i < menulist1.size(); i++) {
				menu menu1 = menulist1.get(i);
				MenuVo menuvo1 = new MenuVo();
				menuvo1.setTitle(menu1.getMenuName());
				menuvo1.setValue(menu1.getMenuId());
				menuvo1.setData(new MenuVo[0]);
				menuvo1.setChecked(false);
				for (rolemenu menu11 : rolemenu) {
					if (menu11.getMenuId().equals(menu1.getMenuId())) {
						menuvo1.setChecked(true);
					}
				}
				menuvolist2[i] = menuvo1;
			}
			menuvo.setData(menuvolist2);
			menuvol[j] = menuvo;

		}
		for (MenuVo menuVo : menuvol) {
			System.out.println(menuVo.toString());
		}
		return menuvol;
	}

	/**
	 * 角色菜单更新
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("editrolemenu")
	@ResponseBody
	public String updaterolemenu(String[] ids) {

		List<menu> menulist = menuservice.selectbymenuparentid(ids);
		List<menu> menulist1=menuservice.selectids(ids);
		List<menu> idlist = new ArrayList<menu>();//现在的菜单表
		idlist.addAll(menulist);
		idlist.addAll(menulist1);
		Iterator<menu> iditer=idlist.iterator();
		Iterator<rolemenu>  rolemenuiter=rolemenu.iterator();
		while(rolemenuiter.hasNext()){
			rolemenu rm=rolemenuiter.next();
			while(iditer.hasNext()){
				
				menu m=iditer.next();
				if(rm.getMenuId().equals(m.getMenuId())){
					rolemenuiter.remove();
					iditer.remove();
				}
			}
		}
		
		 
		if(idlist.size()>0){
			//添加
			rolemenuservice.inserts(idlist,roleid);
		}
		for (cn.com.hfuu.module.rolemenu menu : rolemenu) {
			System.out.println(menu.toString());
		}
		if(rolemenu.size()>0){
			rolemenuservice.deletes(rolemenu);
		}
		
		
		
		return "success";

	}

}
